package com.yami.shop.cp.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.cp.comment.dto.CPProdInfo;
import com.yami.shop.cp.comment.dto.CPProdInfoAndCountDto;
import com.yami.shop.cp.comment.dto.CPSubmitPersonCount;
import com.yami.shop.cp.comment.dto.CollaborativePurchaseMergerDto;
import com.yami.shop.cp.comment.enums.CollaborativePurchaseBasketEnum;
import com.yami.shop.cp.comment.model.CollaborativePurchase;
import com.yami.shop.cp.comment.param.CPSearchParam;
import com.yami.shop.cp.comment.service.CollaborativePurchaseBasketService;
import com.yami.shop.cp.comment.service.CollaborativePurchaseService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("/platform/cp")
public class CollaborativePurchaseController {

    private CollaborativePurchaseService collaborativePurchaseService;

    private CollaborativePurchaseBasketService collaborativePurchaseBasketService;

    @GetMapping("/searchCP")
    @ApiOperation(value = "平台搜索协同采购单")
    public ResponseEntity<IPage<CollaborativePurchase>> searchCP(PageParam page, CPSearchParam cpSearchParam) {
        page.addOrder(OrderItem.desc("create_time"));
        IPage<CollaborativePurchase> collaborativePurchaseIPage = collaborativePurchaseService.getSearchCP(page, cpSearchParam);
        return ResponseEntity.ok(collaborativePurchaseIPage);
    }

    @GetMapping("/searchCP/all")
    @ApiOperation(value = "平台条件搜索协同采购单(不分页)")
    public ResponseEntity<List<CollaborativePurchase>> searchCP(CPSearchParam cpSearchParam) {
        return ResponseEntity.ok(collaborativePurchaseService.getSearchCP(cpSearchParam));
    }


    @GetMapping("/fuzzy/search/id/{collaborativePurchaseId}")
    @ApiOperation(value = "模糊搜索协助采购单id")
    public ResponseEntity<List<String>> fuzzySearchId(@PathVariable("collaborativePurchaseId") String collaborativePurchaseId) {
        List<CollaborativePurchase> collaborativePurchaseList = collaborativePurchaseService.list(new LambdaQueryWrapper<CollaborativePurchase>().like(CollaborativePurchase::getId, collaborativePurchaseId));
        List<String> ids = collaborativePurchaseList.stream().map(CollaborativePurchase::getId).collect(Collectors.toList());
        return ResponseEntity.ok(ids);
    }

    @GetMapping("/fuzzy/search/name/{collaborativePurchaseName}")
    @ApiOperation(value = "模糊搜索协助采购单名称")
    public ResponseEntity<List<String>> fuzzySearchName(@PathVariable("collaborativePurchaseName") String collaborativePurchaseName) {
        List<CollaborativePurchase> collaborativePurchaseList = collaborativePurchaseService.list(new LambdaQueryWrapper<CollaborativePurchase>().like(CollaborativePurchase::getName, collaborativePurchaseName));
        List<String> names = collaborativePurchaseList.stream().map(CollaborativePurchase::getName).collect(Collectors.toList());
        return ResponseEntity.ok(names);
    }

    @GetMapping("/{collaborativePurchaseId}/spInfoAndCount")
    @ApiOperation(value = "根据协同采购id查询提报人详情和统计")
    public ResponseEntity<CPSubmitPersonCount> getCPSubmitPersonCount(@PathVariable("collaborativePurchaseId") String collaborativePurchaseId){
        return ResponseEntity.ok(collaborativePurchaseService.getCPSubmitPersonCount(collaborativePurchaseId));
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据协同采购单id查询采购单详情")
    public ResponseEntity<CollaborativePurchase> collaborativePurchaseById(@PathVariable("id") String collaborativePurchaseId) {
        CollaborativePurchase collaborativePurchase = collaborativePurchaseService.getById(collaborativePurchaseId);
        return ResponseEntity.ok(collaborativePurchase);
    }

    @GetMapping("/cpProdInfoAndCount")
    @ApiOperation(value = "根据协同采购id查看商品下单情况")
    public ResponseEntity<CPProdInfoAndCountDto> getCPProdInfoAndCount(@RequestParam(value = "cpId", required = true) String cpId) {
        List<CPProdInfo> cpProdInfos = collaborativePurchaseBasketService.getCPSubmitAndCompletePordInfo(cpId);
        CPProdInfoAndCountDto cpProdInfoAndCountDto = new CPProdInfoAndCountDto();
        cpProdInfoAndCountDto.setCpProdInfos(cpProdInfos);
        for (CPProdInfo cpProdInfo : cpProdInfos) {
            if (Objects.equals(CollaborativePurchaseBasketEnum.SUBMIT.getValue(), cpProdInfo.getStatus())) {
                cpProdInfoAndCountDto.setSubmitProdCount(cpProdInfoAndCountDto.getSubmitProdCount() + cpProdInfo.getBasketCount());
                cpProdInfoAndCountDto.setSubmitProdTotalAmount(Arith.add(cpProdInfoAndCountDto.getSubmitProdTotalAmount(), Arith.mul(cpProdInfo.getPrice(), cpProdInfo.getBasketCount())));
            }
            if (Objects.equals(CollaborativePurchaseBasketEnum.COMPLETE.getValue(), cpProdInfo.getStatus())) {
                cpProdInfoAndCountDto.setCompleteProdCount(cpProdInfoAndCountDto.getCompleteProdCount() + cpProdInfo.getBasketCount());
                cpProdInfoAndCountDto.setCompleteProdTotalAmount(Arith.add(cpProdInfoAndCountDto.getCompleteProdTotalAmount(), Arith.mul(cpProdInfo.getPrice(), cpProdInfo.getBasketCount())));
            }
        }
        return ResponseEntity.ok(cpProdInfoAndCountDto);
    }


}
